package com.example.administrator.familfinalcialmanagement.ui;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityRegisterBinding;
import com.example.administrator.familfinalcialmanagement.ui.view.RegisterTime;
import com.example.administrator.familfinalcialmanagement.util.ApiHTTP;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/3/26.
 */

public class RegisterActivity extends BaseActivity {

    ActivityRegisterBinding binding;
    RegisterTime timeUtil;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_register);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        setBack();
        setTitleText("注册");

        binding.textViewUpdata.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeUtil != null) {
            timeUtil.onCancel();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textViewUpdata:

                String name = binding.etModuleName.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    ToastUtil.showToast("请输入账户");
                    return;
                }

                String phone = binding.etModulePhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast("请输入手机号码");
                    return;
                }

                String pwd = binding.etModulePwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast("请输入密码");
                    return;
                }

                String pwdAgain = binding.etModulePwdAgain.getText().toString().trim();
                if (TextUtils.isEmpty(pwdAgain)) {
                    ToastUtil.showToast("请再次输入密码");
                    return;
                }

                if (!pwdAgain.equals(pwd)) {
                    ToastUtil.showToast("两次密码输入不一致请重新输入");
                    binding.etModulePwdAgain.setText("");
                    return;
                }

                String email = binding.etModuleEmail.getText().toString().trim();
                if (TextUtils.isEmpty(email)) {
                    ToastUtil.showToast("请输入邮箱");
                    return;
                }

                LogUtil.e("开始联网");
                ApiHTTP.getInstance().register(name, phone, pwd, email, new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtil.e("联网成功"+ s);
                        finish();
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        LogUtil.e("联网失败"+ e.getMessage());
                        ToastUtil.showToast("联网失败"+ e.getMessage());
                    }
                });

                break;
            default:
        }
    }
}
